//
//  JKRFeedDetailCommentCell.m
//  JKRFeedDemo
//
//  Created by Lucky on 2017/12/9.
//  Copyright © 2017年 Lucky. All rights reserved.
//

#import "JKRFeedDetailCommentCell.h"
#import "JKRFeedDetailHelper.h"

@interface JKRFeedDetailCommentCell ()

@property (nonatomic, strong) UIView *lineView;
@property (nonatomic, strong) UIImageView *headerImageView;
@property (nonatomic, strong) YYLabel *nameLabel;
@property (nonatomic, strong) YYLabel *timeLabel;
@property (nonatomic, strong) YYLabel *commentLabel;

@end

@implementation JKRFeedDetailCommentCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    self.selectionStyle = UITableViewCellSelectionStyleNone;
    
    @weakify(self);
    _lineView = [UIView new];
    _lineView.backgroundColor = UIColorHex(E1E1E1);
    [self addSubview:_lineView];
    _headerImageView = [UIImageView new];
    _headerImageView.userInteractionEnabled = YES;
    [_headerImageView addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(headerImageViewDidClick)]];
    [self addSubview:_headerImageView];
    _nameLabel = [self _creatYYLabel];
    _nameLabel.textVerticalAlignment = YYTextVerticalAlignmentBottom;
    _nameLabel.highlightTapAction = ^(UIView * _Nonnull containerView, NSAttributedString * _Nonnull text, NSRange range, CGRect rect) {
        [weak_self highLightClickWithText:text range:range];
    };
    [self addSubview:_nameLabel];
    _timeLabel = [self _creatYYLabel];
    _timeLabel.textVerticalAlignment = YYTextVerticalAlignmentBottom;
    [self addSubview:_timeLabel];
    _commentLabel = [self _creatYYLabel];
    _commentLabel.highlightTapAction = ^(UIView * _Nonnull containerView, NSAttributedString * _Nonnull text, NSRange range, CGRect rect) {
        [weak_self highLightClickWithText:text range:range];
    };
    [self addSubview:_commentLabel];
    return self;
}

- (void)highLightClickWithText:(NSAttributedString *)text range:(NSRange)range {
    YYTextHighlight *highlight = [text attribute:YYTextHighlightAttributeName atIndex:range.location];
    NSDictionary *info = highlight.userInfo;
    if (info.count == 0) {
        return;
    }
    if (info[kHightLightTapUser]) {
        if (self.delegate && [self.delegate respondsToSelector:@selector(feedCommentDidClickUserWithUserID:)]) {
            [self.delegate feedCommentDidClickUserWithUserID:info[kHightLightTapUser]];
        }
        return;
    }
    if (info[kHightLightTapFeedComment]) {
        if (self.delegate && [self.delegate respondsToSelector:@selector(feedCommentReplayWithCommentID:)]) {
            [self.delegate feedCommentReplayWithCommentID:info[kHightLightTapFeedComment]];
        }
        return;
    }
}

- (void)headerImageViewDidClick {
    if (self.layout.model.author && self.delegate && [self.delegate respondsToSelector:@selector(feedCommentDidClickUserWithUserID:)]) {
        [self.delegate feedCommentDidClickUserWithUserID:self.layout.model.author.uid];
    }
}

- (void)setLayout:(JKRFeedCommentLayout *)layout {
    _layout = layout;
    self.lineView.frame = layout.topLineFrame;
    self.headerImageView.frame = layout.headerImageViewFrame;
    [self.headerImageView setImageWithURL:[NSURL URLWithString:layout.model.author.thumbnail] placeholder:nil options:kNilOptions manager:[JKRFeedDetailHelper circleHeaderImageManager] progress:nil transform:nil completion:nil];
    self.nameLabel.frame = layout.nameLabelFrame;
    self.nameLabel.textLayout = layout.nameLabelLayout;
    self.timeLabel.frame = layout.timeLabelFrame;
    self.timeLabel.textLayout = layout.timeLabelLayout;
    self.commentLabel.frame = layout.commentLabelFrame;
    self.commentLabel.textLayout = layout.commentLabelLayout;
}

- (YYLabel *)_creatYYLabel {
    YYLabel *label = [YYLabel new];
    label.ignoreCommonProperties = YES;
    label.fadeOnAsynchronouslyDisplay = YES;
    label.textVerticalAlignment = YYTextVerticalAlignmentCenter;
    return label;
}

@end
